package org.example.chatroom.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.chatroom.common.pojo.dataobject.Message;
import org.example.chatroom.common.pojo.dataobject.UserInfo;
import org.example.chatroom.common.pojo.response.MessageResponse;
import org.example.chatroom.mapper.MessageMapper;
import org.example.chatroom.mapper.UserMapper;
import org.example.chatroom.service.MessageService;
import org.example.chatroom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2025-06-26
 * Time: 16:05
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Override
    public List<MessageResponse> getmessage(Integer dialogueId) {
        //查找用户id 发送内容 发送时间
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Message::getDialogueId,dialogueId)
                .orderByAsc(Message::getSendTime);
        List<Message> messages = messageMapper.selectList(queryWrapper);
        List<MessageResponse> messageResponses =new ArrayList<>();
        for(Message message : messages){
            QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
            userInfoQueryWrapper.lambda().eq(UserInfo::getId,message.getUserId());
            UserInfo userInfo = userMapper.selectOne(userInfoQueryWrapper);
            messageResponses.add(new MessageResponse(userInfo.getUserName(),userInfo.getAvatarUrl(),
                    message.getContent(),message.getSendTime()));
        }
        return messageResponses;

    }

    @Override
    public void add(Message message) {
        messageMapper.insert(message);
    }
}
